package org.example.mq;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class LogProduct {


    private final RocketMQTemplate rocketMQTemplate;

    public LogProduct(RocketMQTemplate rocketMQTemplate) {
        this.rocketMQTemplate = rocketMQTemplate;
    }


    public void send(OperationLogEvent log) {
//        // 生成唯一ID，防止重复消费
//        String messageId = IdUtil.fastSimpleUUID();
//        log.setMessageId(messageId);

        // 同步发送，获取发送结果
        SendResult sendResult = rocketMQTemplate.syncSend("log-topic", log);

        if (sendResult.getSendStatus() != SendStatus.SEND_OK) {
            throw new RuntimeException("日志消息发送失败: " + sendResult);
        }

        System.out.println("日志消息发送成功: " + sendResult);
    }
}
